var { Plot } = require("../Plot.js")
// @ts-ignore
var plot = new Plot(document.getElementById("myCanvas"));

var circleXshaft = 200;
var circleYshaft = 300;
plot.context.ellipse(0, 0, circleYshaft, circleXshaft, 0, 0, Math.PI * 1.5);
var ctx = plot.context;
ctx.fillStyle = "#058";
ctx.strokeStyle = "#000";
ctx.fill();
ctx.stroke();

plot.container.addEventListener("touchstart", function (tvs) {
	if (tvs.touches.length == 2) {
		var point1 = {
			x: tvs.touches[0].clientX - plot.container.width / 2,
			y: plot.container.height / 2 - tvs.touches[0].clientY
		}

		var point2 = {
			x: tvs.touches[1].clientX - plot.container.width / 2,
			y: plot.container.height / 2 - tvs.touches[1].clientY
		}
		var a1 = point1.x, b1 = point1.y,
			a2 = point2.x, b2 = point2.y;
		var p1 = (-a1 ^ 2 * b2 ^ 2) + 2 * a1 * a2 * b1 * b2 - a2 ^ 2 * b1 ^ 2;
		var p2 = b2 ^ 2 - 2 * b1 * b2 + b1 ^ 2;
		var p3 = (a2 ^ 2 - 2 * a1 * a2 + a1 ^ 2);
		var p4 = Math.sqrt(b * (a * p1 + a ^ 2 * p2) + a * p3 * b ^ 2);

		var a = circleYshaft ** 2;
		var b = circleXshaft ** 2;

		var intersection1 = {};
		var intersection2 = {};
		intersection1.x = ((-a2 * p4) + a1 * p4 + a * (a1 * b2 ^ 2 + ((-a2) - a1) * b1 * b2 + a2 * b1 ^ 2)) / (a * p2 + p3 * b);
		intersection1.y = -(b2 * p4 - b1 * p4 + b * ((a1 * a2 - a1 ^ 2) * b2 + (a1 * a2 - a2 ^ 2) * b1)) / (a * p2 + p3 * b);


		intersection2.x = (a2 * p4 - a1 * p4 + a * (a1 * b2 ^ 2 + ((-a2) - a1) * b1 * b2 + a2 * b1 ^ 2)) / (a * p2 + p3 * b);
		intersection2.y = -((-b2 * p4) + b1 * p4 + b * ((a1 * a2 - a1 ^ 2) * b2 + (a1 * a2 - a2 ^ 2) * b1)) / (a * p2 + p3 * b)

		plot.line(intersection1, intersection2, "green")
	}
})


plot.line({ x: 0, y: circleYshaft }, { x: circleXshaft, y: 0 }, "yellow")


